<template>
    <div class="delay">
        <div class="title">拖期配置</div>
        <div class="table-name">配置1周期</div>
        <w-table :isShowPagination="false" :propTableConfig="delayTableOneConfig" ref="tableOneRef">
            <template #operate="{ scope }">
                <el-button link type="primary" size="small" @click="handler(scope.row)">操作</el-button>
            </template>
            <template #createTime>
                <span>工单生成时间</span>
            </template>
            <template #closeTime>
                <span>工单生成时间</span>
            </template>
        </w-table>
        <div class="table-name">配置2周期</div>
        <w-table :isShowPagination="false" :propTableConfig="delayTableTwoConfig" ref="tableTwoRef">
            <template #operate="{ scope }">
                <el-button link type="primary" size="small" @click="handler(scope.row)">操作</el-button>
                <el-button link type="primary" size="small" @click="del(scope.row)">删除</el-button>
            </template>
            <template #createTime>
                <span>工单生成时间</span>
            </template>
            <template #closeTime>
                <span>工单生成时间</span>
            </template>
        </w-table>
        <div class="add" @click="add">
            <i class="iconfont icon-a-2"></i>
        </div>
        <w-popup :isShowPopup="isShowPopup" @close="isShowPopup = false" @confrim="confrim" :title="title" :loading="popLoading">
            <div v-if="title === '删除'">确定要删除吗？</div>
            <w-form :formConfig="delayConfig" :formData="popFormData" :rules="popupRules" ref="wFormRef"></w-form>
        </w-popup>
    </div>
</template>

<script setup>
import { provide, ref } from "vue";
import wTable from "@/components/wTable.vue";
import wPopup from "@/components/wPopup.vue";
import { delayTableOneConfig, delayTableTwoConfig, delayFormAddConfig, delayOneFormSetConfig } from "./config.js";
import { getDelayList, setDelay, addDelay, getReportDist, delDelay } from "@/service/api/report.js";

const isShowPopup = ref(false);
const popLoading = ref(false);
const delayConfig = ref([]);
const title = ref("");
const popFormData = ref({});
const popFormOneData = { id: 0, cycle: "" };
const popFormAddData = { workOrderLevel: "", cycle: "" };
const tableOneRef = ref(null);
const tableTwoRef = ref(null);
const handler = (data) => {
    title.value = "修改拖期配置";
    delayConfig.value = delayOneFormSetConfig;
    popFormData.value = popFormOneData;
    popFormData.value.id = data.id;
    popFormData.value.cycle = data.cycle;
    isShowPopup.value = true;
};
const del = (data) => {
    title.value = "删除";
    popFormData.value = {};
    delayConfig.value = [];
    isShowPopup.value = true;
    popFormOneData.id = data.id;
};
const add = () => {
    title.value = "添加拖期配置";
    popFormData.value = popFormAddData;
    delayConfig.value = delayFormAddConfig;
    isShowPopup.value = true;
};
const confrim = () => {
    popLoading.value = true;
    if (title.value === "修改拖期配置") {
        setDelay(popFormData.value)
            .then((res) => {
                popLoading.value = false;
                if (res.code === 200) {
                    ElMessage({
                        message: "修改成功",
                        type: "success"
                    });
                    getDelayListFn(1);
                    getDelayListFn(2);
                }
                isShowPopup.value = false;
            })
            .catch(() => (popLoading.value = false));
    } else if (title.value === "添加拖期配置") {
        addDelay(popFormData.value)
            .then((res) => {
                popLoading.value = false;
                if (res.code === 200) {
                    ElMessage({
                        message: "添加成功",
                        type: "success"
                    });
                    getDelayListFn(1);
                    getDelayListFn(2);
                }
                isShowPopup.value = false;
            })
            .catch(() => (popLoading.value = false));
    } else {
        delDelay({ id: popFormOneData.id })
            .then((res) => {
                popLoading.value = false;
                if (res.code === 200) {
                    ElMessage({
                        message: "删除成功",
                        type: "success"
                    });
                    getDelayListFn(2);
                }
                isShowPopup.value = false;
            })
            .catch(() => (popLoading.value = false));
    }
};
getDelayListFn(1);
getDelayListFn(2);
function getDelayListFn(i) {
    getDelayList(i).then((res) => {
        if (res.code === 200) {
            i === 1 ? (tableOneRef.value.tableData = res.data) : (tableTwoRef.value.tableData = res.data);
        }
    });
}
getReportDist({ parentCode: "Repair_Order_Level" }).then((res) => {
    if (res.code === 200) {
        delayFormAddConfig[0].options = res.data.map((item) => ({ label: item.name, value: item.value }));
    }
});
provide("isShowSelect", false);
provide("isShowBorder", true);
</script>

<style lang="less" scoped>
.delay {
    padding: 16px;
    background-color: #fff;
    font-size: 16px;
    color: #1b3646;
    .title {
        font-weight: 600;
    }
    .table-name {
        margin: 24px 0 16px 0;
        font-weight: 400;
    }
    .add {
        cursor: pointer;
        i {
            font-size: 30px;
        }
    }
}
</style>
